/**
 * Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. 
For example, the longest substring without repeating letters for "abcabcbb" is "abc", 
which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
 */
package com.xinpan.exercise;

import java.util.ArrayList;
import java.util.List;


public class LongestSubstringWithoutRepeating {
    public int lengthOfLongestSubstring(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
        List<Integer> visitQ = new ArrayList<Integer>();

        int max = 0;
        for(int i = 0; i < s.length(); i++)
        {
            int val = (int)s.charAt(i);
            if(visitQ.contains(val))
            {
                for(;visitQ.get(0) != val;)
                    visitQ.remove(0);
                visitQ.remove(0);
            }

            visitQ.add(val);
            if(visitQ.size() > max)
                max = visitQ.size();

        }
        return max;
    }
    
    public static void main(String[] args)
    {
    	LongestSubstringWithoutRepeating lswp = new LongestSubstringWithoutRepeating();
    	lswp.lengthOfLongestSubstring("abcabcde");
    }
}
